package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.incar.chuxing.R;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.ExchangeCouponBean;
import com.incar.chuxing.utils.Constants;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.StatusBarUtil;
import com.incar.chuxing.utils.Toast.ToastCompat;
import com.incar.chuxing.utils.zxing.android.CaptureActivity;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ExchangeCouponsActivity extends BaseActivity {


    @BindView(R.id.editText_myCoupon)
    EditText editTextMyCoupon;
    @BindView(R.id.button_myCoupon)
    TextView buttonMyCoupon;
    private Context mContext = this;
    private List<Fragment> mlist;
    private String[] arrTabTitles;
    private String code;
    @BindView(R.id.title_name)
    TextView titleName;
    @BindView(R.id.title_more)
    ImageView titleMore;
    @Override
    public int getLayoutId() {
        return R.layout.activity_my_exchangecoupon;
    }

    @Override
    public void initView() {
        titleName.setText("兑换优惠券");
        titleMore.setImageResource(R.drawable.scan_grey);
    }

    @Override
    public void initData() {
        super.initData();

    }

    private void loadNetworkData(int tabIndex) {
        if (((MyApplication) getApplication()).getUid() == null || ((MyApplication) getApplication()).getUid().equals("")) {
            ToastCompat.makeText(mContext, getResources().getString(R.string.account_expire), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            return;

        }


    }

    @OnClick({R.id.title_back, R.id.button_myCoupon,R.id.title_more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.button_myCoupon:
                code = editTextMyCoupon.getText().toString();
                if (!TextUtils.isEmpty(code)) {
                    exchangeCoupon(code);
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.inputExchange), Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.title_more:
                Intent intent = new Intent(this, CaptureActivity.class);
                intent.putExtra("title", "扫描优惠券");
                startActivityForResult(intent, Constants.REQUEST_SCAN_COUPON);
                break;

        }
    }


    private void exchangeCoupon(String code) {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).exchangeCoupon(((MyApplication) getApplication()).getRequestFieldMap(), code).enqueue(new Callback<ExchangeCouponBean>() {
            @Override
            public void onResponse(Call<ExchangeCouponBean> call, Response<ExchangeCouponBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        ToastCompat.makeText(mContext, getResources().getString(R.string.exchangeSuccess), Toast.LENGTH_SHORT).show();
                        initView();
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<ExchangeCouponBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }


}
